package Demo1;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Demo {
    // 反射私有的构造方法 屏蔽内容为获得公有的构造方法
    public static void reﬂectNewInstance() {
        Class classStudent = null;
        try {
            // 实例化对象
            Class<?> classStudentStudent = classStudent.forName("Demo1.Student");
            Student student = (Student)classStudentStudent.newInstance();
            System.out.println(student);
            // 调用私有的构造方法
            Constructor<?> constructor = classStudentStudent.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);
            Student student1 = (Student) constructor.newInstance("张三",18);
            System.out.println(student1);

        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }

    }
    // 反射私有属性
    public static void reflectPrivateField() {
        Class classStudent = null;
        try {
            // 实例化对象
            Class<?> classStudentStudent = classStudent.forName("Demo1.Student");
            Field field = classStudentStudent.getDeclaredField("name");
            field.setAccessible(true);
            Student student = (Student) classStudentStudent.newInstance();

            field.set(student,"张三");
            System.out.println(student);


        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    // 反射私有方法
    public static void refiectPrivateMethod() {
        Class c1 = null;
        try {
            c1 = Class.forName("Demo1.Student");
            Method method = c1.getDeclaredMethod("eat");
            method.setAccessible(true);
            Student student = (Student) c1.newInstance();
            method.invoke(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        //reﬂectNewInstance();
        // 反射私有属性
        //reflectPrivateField();
        //反射私有方法
        refiectPrivateMethod();
    }

    public static void main1(String[] args) {
        // Class 只有一份
        Class c1 = null;
        try {
            c1 = Class.forName("Demo1.Student");
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Class c2 = Student.class;

        Student student = new Student();
        Class c3 = student.getClass();
        System.out.println(c1 == c2);
        System.out.println(c1 == c3);
    }
}
